#include "../includes.h"


unsigned long LastRamDumpAddress = 0x000;

int NAVI_IsEqual(char *, char *);

extern void send_version();

void NAVICOMCommand(char);
char DoNAVICOMCommand(int, char *,char, char);
CMD_ NAVICOMGetCommandID(unsigned char *,int);
void reset_sub_call(void);
void quick_reset(void);
void MemoryHexDump( void *address, int size );

char * GetBaudrateString(char);
char GetBaudrate(char *);
char * GetPositionString(P_	);

char disp_memory_usage = 0;
unsigned char preFPGAAddr=0;

void DisplayHelp(void);

char ret_buf[512];

char dual_print_9927 = 0;

Navi_cmd_ Navicmd[NAVICOM_COMMAND_SUB_ID_NO] =
{	{ "OPER",             0,	  0,	  0,	 19,	  1 },
	{ "STATUS",           0,	  1,	  2,	  4,	  2 },
	{ "DUAL",             0,	  3,	  0,	  3,	  0 },
	{ "SYNC",             0,	  4,	  0,	  0,	  0 },
	{ "ERROR",            0,	  5,	  0,	  5,	  0 },
	{ "LOG",              0,	  6,	  7,	 10,	  6 },
	{ "CLEAR",            8,	  0,	  0,	  7,	  0 },
	{ "COUNT",            0,	  9,	  0,	  8,	  0 },
	{ "TEST",             0,	  0,	 10,	  9,	  0 },
	{ "ALL",              0,	 11,	  0,	  0,	  0 },
	{ "TLOG",             0,	 12,	  0,	 15,	 11 },
	{ "CLEAR",           13,	  0,	  0,	 12,	  0 },
	{ "COUNT",            0,	 14,	  0,	 13,	  0 },
	{ "TEST",             0,	  0,	 15,	 14,	  0 },
	{ "ALL",              0,	 16,	  0,	  0,	  0 },
	{ "MODE",             0,	  0,	 17,	 16,	  0 },
	{ "DUAL",             0,	  0,	  0,	 18,	 17 },
	{ "ACTIVE",           0,	  0,	 18,	  0,	  0 },
	{ "MANUAL",           0,	 19,	 20,	  0,	  0 },
	{ "GPS",              0,	  0,	  0,	 38,	 20 },
	{ "TIME",             0,	 21,	 22,	 21,	  0 },
	{ "DATE",             0,	 23,	 24,	 22,	  0 },
	{ "TRACK",            0,	 25,	  0,	 29,	 23 },
	{ "DOP",              0,	 26,	  0,	 24,	  0 },
	{ "SAT",              0,	 27,	  0,	 26,	 25 },
	{ "COUNT",            0,	 28,	  0,	  0,	  0 },
	{ "MASK",             0,	 29,	 30,	 27,	  0 },
	{ "COUNTER",          0,	  0,	  0,	  0,	 28 },
	{ "CLEAR",           31,	  0,	  0,	  0,	  0 },
	{ "POS",              0,	 32,	  0,	  0,	 30 },
	{ "MODE",             0,	 33,	  0,	 31,	  0 },
	{ "CURRENT",          0,	 34,	  0,	 32,	  0 },
	{ "LAST",             0,	 35,	  0,	 33,	  0 },
	{ "HOLD",             0,	  0,	 36,	 34,	  0 },
	{ "FIX",              0,	 37,	 38,	 35,	  0 },
	{ "AVERAGE",          0,	 39,	  0,	  0,	 36 },
	{ "START",           40,	  0,	  0,	 37,	  0 },
	{ "END",             41,	  0,	  0,	  0,	  0 },
	{ "TIME",             0,	  0,	  0,	 48,	 39 },
	{ "ZONE",             0,	 42,	 43,	 40,	  0 },
	{ "MODE",             0,	 44,	 45,	 41,	  0 },
	{ "LEAPSECOND",       0,	 46,	  0,	 42,	  0 },
	{ "ADELAY",           0,	 47,	 48,	 43,	  0 },
	{ "CODE",             0,	 49,	 50,	 44,	  0 },
	{ "RAIM",             0,	 51,	 52,	 45,	  0 },
	{ "UTC",              0,	 53,	  0,	 46,	  0 },
	{ "GPS",              0,	 54,	  0,	 47,	  0 },
	{ "RTC",              0,	 55,	 56,	  0,	  0 },
	{ "SYSTEM",           0,	  0,	  0,	 63,	 49 },
	{ "SELFTEST",        57,	 58,	  0,	 50,	  0 },
	{ "RESET",           59,	  0,	 60,	 51,	  0 },
	{ "COMM",             0,	 61,	  0,	 53,	 52 },
	{ "BAUD",             0,	 62,	 63,	  0,	  0 },
	{ "FIRMWARE",         0,	  0,	  0,	 56,	 54 },
	{ "VERSION",          0,	 64,	  0,	 55,	  0 },
	{ "UPGRADE",         65,	  0,	 66,	  0,	  0 },
	{ "CONFIG",           0,	 67,	  0,	 57,	  0 },
	{ "INFO",             0,	 68,	 69,	 58,	  0 },
	{ "SN",               0,	 70,	 71,	 59,	  0 },
	{ "FW",               0,	  0,	  0,	 61,	 60 },
	{ "VERSION",          0,	 72,	  0,	  0,	  0 },
	{ "VERSION",          0,	 73,	  0,	 62,	  0 },
	{ "REVISION",         0,	 74,	 75,	  0,	  0 },
	{ "DEV",              0,	  0,	  0,	 78,	 64 },
	{ "SIG",              0,	 76,	 77,	 65,	  0 },
	{ "MEM",              0,	 78,	 79,	 66,	  0 },
	{ "LSI",              0,	  0,	 80,	 67,	  0 },
	{ "CONFIG",           0,	  0,	  0,	 75,	 68 },
	{ "DAC",              0,	 81,	 82,	 69,	  0 },
	{ "GAIN",             0,	 83,	 84,	 70,	  0 },
	{ "OCXO",             0,	 85,	 86,	 71,	  0 },
	{ "DELAY",            0,	 87,	 88,	 72,	  0 },
	{ "E1",               0,	  0,	  0,	  0,	 73 },
	{ "ACC",              0,	 89,	 90,	 74,	  0 },
	{ "JITTER",           0,	 91,	 92,	  0,	  0 },
	{ "PRINT",            0,	  0,	  0,	 76,	  0 },
	{ "TEST",             0,	 93,	 94,	 77,	  0 },
	{ "OFFSET",           0,	 95,	 96,	  0,	  0 },
	{ "CONTROL",          0,	  0,	  0,	 92,	 79 },
	{ "DAC",              0,	 97,	 98,	 80,	  0 },
	{ "MANUAL",           0,	 99,	100,	 81,	  0 },
	{ "TGAIN",            0,	101,	102,	 82,	  0 },
	{ "RDAC",             0,	103,	104,	 83,	  0 },
	{ "SYNC",           105,	  0,	106,	 84,	  0 },
	{ "HOLDOVER",         0,	  0,	  0,	 86,	 85 },
	{ "DURATION",         0,	107,	108,	  0,	  0 },
	{ "OCXOID",           0,	109,	  0,	 87,	  0 },
	{ "1PPSALM",          0,	110,	  0,	 89,	 88 },
	{ "RANGE",            0,	111,	112,	  0,	  0 },
	{ "WARM",             0,	  0,	  0,	 91,	 90 },
	{ "RANGE",            0,	113,	114,	  0,	  0 },
	{ "TRACE",            0,	115,	116,	  0,	  0 },
	{ "MEM",              0,	  0,	  0,	 95,	 93 },
	{ "READ",             0,	  0,	117,	 94,	  0 },
	{ "WRITE",            0,	  0,	118,	  0,	  0 },
	{ "TEST",             0,	  0,	  0,	100,	 96 },
	{ "OCXOID",           0,	119,	  0,	 97,	  0 },
	{ "EB",               0,	120,	  0,	 98,	  0 },
	{ "VERSION",          0,	121,	  0,	 99,	  0 },
	{ "OFFSET",           0,	  0,	122,	  0,	  0 },
	{ "RECOVERY",         0,	  0,	  0,	102,	101 },
	{ "OFFSET",           0,	123,	  0,	  0,	  0 },
	{ "CLS",            124,	  0,	  0,	103,	  0 },
	{ "HELP",           125,	  0,	  0,	104,	  0 },
	{ "DETAIL",         126,	  0,	  0,	105,	  0 },
	{ "PECL",             0,	  0,	  0,	108,	106 },
	{ "CHECK",            0,	127,	128,	  0,	107 },
	{ "STATUS",           0,	129,	  0,	  0,	  0 },
	{ "PRINT",            0,	  0,	  0,	115,	109 },
	{ "EB",               0,	130,	131,	110,	  0 },
	{ "DUAL",             0,	132,	133,	111,	  0 },
	{ "HOLDOVER",         0,	134,	135,	112,	  0 },
	{ "DEBUG",            0,	136,	137,	113,	  0 },
	{ "ATABLE",         138,	  0,	  0,	114,	  0 },
	{ "ASOLUTION",      139,	  0,	  0,	  0,	  0 },
	{ "HOLDOVER",         0,	  0,	  0,	117,	116 },
	{ "SOLUTION",       140,	  0,	  0,	  0,	  0 },
	{ "FUPGRADEL",      141,	  0,	  0,	118,	  0 },
	{ "FUPGRADEM",      142,	  0,	  0,	119,	  0 },
	{ "FUPGRADET",      143,	  0,	  0,	120,	  0 },
	{ "FUPGRADEG",      144,	  0,	  0,	121,	  0 },
	{ "DEBUG",            0,	  0,	  0,	126,	122 },
	{ "FAIL",             0,	145,	  0,	  0,	123 },
	{ "RTC",              0,	  0,	146,	124,	  0 },
	{ "OSC",              0,	  0,	147,	125,	  0 },
	{ "POWER",            0,	  0,	148,	  0,	  0 },
	{ "TANT",             0,	  0,	  0,	129,	127 },
	{ "RESET",          149,	  0,	150,	128,	  0 },
	{ "VERSION",          0,	151,	  0,	  0,	  0 },
	{ "EDBG",             0,	152,	153,	130,	  0 },
	{ "ANTENNA",          0,	  0,	  0,	132,	131 },
	{ "CHECK",            0,	154,	155,	  0,	  0 },
	{ "DBG",              0,	  0,	  0,	134,	133 },
	{ "RB",             156,	  0,	  0,	  0,	  0 },
	{ "RFT",            157,	  0,	  0,	135,	  0 },
	{ "RFS",            158,	  0,	  0,	136,	  0 },
	{ "MONITORCMD",       0,	  0,	159,	137,	  0 },
	{ "PWM",              0,	  0,	160,	138,	  0 },
	{ "EB",               0,	  0,	  0,	145,	139 },
	{ "PRINT",            0,	  0,	161,	140,	  0 },
	{ "LOCK",             0,	  0,	162,	141,	  0 },
	{ "UNLOCK",           0,	  0,	163,	142,	  0 },
	{ "PSDM",             0,	  0,	164,	143,	  0 },
	{ "SS",               0,	  0,	165,	144,	  0 },
	{ "SL",               0,	  0,	166,	  0,	  0 },
	{ "MM",               0,	  0,	  0,	149,	146 },
	{ "DUMP",             0,	  0,	167,	147,	  0 },
	{ "CRC",              0,	  0,	168,	148,	  0 },
	{ "USAGE",            0,	  0,	169,	  0,	  0 },
	{ "REBOOT",         170,	  0,	171,	  0,	  0 } }; 

	
	


unsigned long offset_1pps;

unsigned char buf_for_parsing[SINGLE_COMMAND_LENGTH] ;
void NAVICOMCommand(char port)
{

    int 		i,index;
    unsigned char 	CommandFound;
	unsigned char 	SingleCommandBuff[SINGLE_COMMAND_LENGTH];

	CMD_ CommandID;

	int 		*RxCount;
	unsigned char 	*CommandBuffer;
	Command_	*Command;
	PORT_		*PORT;
	
	int rlt;


	switch(port){

		case PORT_CONTROL:
				Command = &COMMAND.CTRL_PORT;
				RxCount = &scc3_rx_cnt;
				CommandBuffer = CommandBuffer3;
				PORT 	= &SYSTEM.IO.CTRL_PORT;
				break;
		case PORT_COMMON:
				return;
				
				Command = &COMMAND.COMMON_PORT;
				RxCount = &scc1_rx_cnt;
				CommandBuffer = CommandBuffer1;
				PORT 	= &SYSTEM.IO.COMMON_PORT;
				break;
	}

    SingleCommandBuff[0] = 0;
	

	for( i = 0, CommandFound = NO; i < *RxCount; i++){

        if (CommandBuffer[i] == 13 || CommandBuffer[i] == 10){
            if( i > 0 ){
            	CommandBuffer[i] = 0;	CommandFound = YES;
            } else {
				*RxCount = 0;
            }
            break;
        }
    }

    if ( CommandFound && *RxCount != 0 ){

		// (1)
		Command->SERVICE = CMD_PARSING;

		// (2)
		sprintf((char*)Command->Input,"%s\r\n",CommandBuffer);
		Command->Output[0] = 0;

        index = 0;
        Command->Count = 0;

        for( i = 0, index = 0; i < *RxCount && index<SINGLE_COMMAND_LENGTH-2; i++ ){

            if( CommandBuffer[i] == ';' || CommandBuffer[i] == 0){
            	
                SingleCommandBuff[index++] = CommandBuffer[i];  SingleCommandBuff[index] = 0;


				//---------------------------------------------------------------------------------
				strcpy(buf_for_parsing, SingleCommandBuff);
				rlt = parsing_command(buf_for_parsing);
				//printf("\r\nrlt=%d", rlt);
				//---------------------------------------------------------------------------------
	

				if( !rlt ){
					CommandID = NAVICOMGetCommandID(SingleCommandBuff,index+1);

            		if (CommandID.ID != 0 && CommandID.ID != -1){
            			// (3)
            			CommandID.Port = port;
                		Command->Queue[Command->Count++] = CommandID;
					}
				}

                index = 0;
            }
            else {
                if( index < SINGLE_COMMAND_LENGTH -1 ){

                	if(CommandBuffer[i] == '_') { CommandBuffer[i] = '/'; }
                    
                    SingleCommandBuff[index++] = CommandBuffer[i];
                }
            }
        }

        *RxCount = 0;

		if(Command->Count == 0){
			////kang--------------------------------------
			//if(CommandBuffer[0] == '/' && CommandBuffer[1] == '\0'){
			//	//strcpy(Command->Output, "Command Over\r\n");
			//}
			//else{
			//	//strcpy(Command->Output, "\r\nSyntax Error");	
			//	//strcpy(Command->Output, "\nGRCA 01>");	
            //
			//}
			////kang strcpy(Command->Output, "Syntax Error\r\n");
			////-----------------------------------------
			//Command->SERVICE = CMD_REPLY;
			if(CommandBuffer[0] == '/' && CommandBuffer[1] == '\0'){
				SYSTEM.IO.CTRL_PORT.MODE = UWAIT_TO_COMMAND;
				PrintON=0;
			}
			
			Command->SERVICE = CMD_REPLY;
			
		}else{
			Command->Current = 0;
			Command->Output[0] = 0;
			Command->SERVICE = CMD_EXECUTION;
		}
    }
    else {
    	if( PORT->PROMPT == ON ){
    		Command->SERVICE = CMD_REPLY;
    	}
    	else {
    		Command->SERVICE = CMD_PRE_INPUT;
    	}

    }

    if(*RxCount >= SCC_BUF_LENGTH-1){
      	*RxCount = 0;
    }
}


int NAVI_IsEqual(char *cmd, char *name)
{
    /* cmd has NULL character */

    int i,length;

    char full_name[20];

    length = strlen(name);

    for( i = 0; i < length; i++){
        full_name[i] = upper(name[i]);
    }

    full_name[i] = 0;
    
    if(!strcmp(cmd,full_name))
        return YES;
    else
        return NO;
}



CMD_ NAVICOMGetCommandID(unsigned char *Buffer,int length)
{
    int i,j,k,found;
    int comm_depth,str_index,id_index ;
    char    CommandPart;

    char Command[10][20];

	CMD_	result;

    comm_depth = str_index = 0;
    CommandPart = COMMAND_INDICATOR;

    for(i = 0; i < 10; i++) 	Command[i][0] = 0;

    result.CommandSet = NAVICOM_COMMAND;
    result.Parameter[0] = 0;
    result.ID = UERROR;
	result.CallCount = 0;

    for(i = 0; i < length+1; i++){

        switch( CommandPart ){

            case    COMMAND_INDICATOR:

                        if(isalpha((int)Buffer[i])){

                            Command[comm_depth][str_index++] = upper((char)Buffer[i]);
                            CommandPart = COMMAND_STRING;
                        }
                        else if(Buffer[i] != ' '){
                            return result;
							}
                        break;

            case    COMMAND_STRING:

                        if(isalpha((int)Buffer[i]) || isdigit((int)Buffer[i]) ){
                        //if(isalpha((int)Buffer[i])){
                            Command[comm_depth][str_index++] = upper((char)Buffer[i]);
                        }
                        else if (Buffer[i] == '/'){
                            Command[comm_depth++][str_index] = 0;
                            str_index = 0;
                        }
                        else if (Buffer[i] == ' '){   /* parameter */
                            Command[comm_depth][str_index] = 0;
                            CommandPart = COMMAND_PARAMETER;
                        }
                        else if (Buffer[i] == '?'){ /* ? */
                            Command[comm_depth][str_index] = 0;
                            CommandPart = COMMAND_QUESTION;
                        }
                        else if (Buffer[i] == ';' || Buffer[i] == 0){ /* command end */
                            Command[comm_depth][str_index] = 0;
                            CommandPart = COMMAND_END;
                        }
                        else{
                            return result;
                        }

                        break;

            case    COMMAND_PARAMETER:
            case    COMMAND_QUESTION:
            case    COMMAND_END:
						
						id_index = 0;	// start point

						for(j = 0; j <= comm_depth; j++){

							found = NO;
							do {
								if(NAVI_IsEqual(Command[j],Navicmd[id_index].name)){
									found = YES;
									if( j!= comm_depth) {id_index = Navicmd[id_index].son;}
								}
								else {
									id_index = Navicmd[id_index].bro;
								}

							} while( id_index!=0 && found == NO );

							if (!found) return result;
						}


						if(found){
							if (CommandPart == COMMAND_QUESTION){
								result.ID = Navicmd[id_index].qid;
								return result;
							}
							else if (CommandPart == COMMAND_END){
								result.ID = Navicmd[id_index].id;
								return result;
							}
							else if (CommandPart == COMMAND_PARAMETER){
								result.ID = Navicmd[id_index].pid;	//ID + Parameter
								for(j = 0; i < length+1; j++,i++)  result.Parameter[j] = upper(Buffer[i]);
								result.Parameter[j] = 0;

								return result;
							}
						}
                        else{

                            return result;
                        }
                        break;
        }
    }
    return result;
}


char DoNAVICOMCommand(int comm, char *Parameter, char count, char port)
{
	int 	t1,t2,t3,l1,l2,l3;
	float   f1,f2,f3,f4;
	long    k1,k2,k3;
	char    d1,d2;
	int 	i,j;
	
	char string[20];
	char message[2048];

	char    baudrate;

	unsigned short s1,s2;

	unsigned short *S_ADDR;
	unsigned char *s_addr;

	char result;
	char* str_model;
	char* str_manu;
	char* str_date;
	char str_ver[40];
	char dummy[16];

    Command_	*Command;
    PORT_		*PORT;


	switch(port){
		case PORT_CONTROL:
					Command = &COMMAND.CTRL_PORT;
					PORT 	= &SYSTEM.IO.CTRL_PORT;
					break;
		case PORT_COMMON:
					Command = &COMMAND.COMMON_PORT;
					PORT 	= &SYSTEM.IO.COMMON_PORT;
					break;
	}


	message[0] = 0;
	string[0] = 0;


	result = EXE_END;

	switch(comm){


		case    OPER_MANUAL_P:
                    if( !strcmp(Parameter,"ON") ){
						MANUAL_START = ON;
						strcpy(message,"Manual Operation Start.");
                    }
                    break;
			
		case	HELP_:
					
					//HELP.DISPLAY_TURN = HELP_FIRST;
					//strcpy(message,HardWareName" Command List & Descriptions");

					//PORT->MODE = UDISPLAY_HELP;
					printf("\r\n"HardWareName" Command List & Descriptions\r\n");
					DisplayHelp();

					break;



		case	CLS_:
					strcpy(message,"x[1Jx[0J");
					message[0] = 0x1B; message[4]=0x1B;
					break;

		case	OPER_STATUS_Q:
					
					PORT->MODE = USTATUS_ASCII;

					strcpy(message,"x[1Jx[0J");
					message[0] = 0x1B; message[4]=0x1B;
					break;
		case	OPER_STATUS_P:
					if( !strcmp(Parameter,"BINARY") ) {
						PORT->MODE = USTATUS_BINARY;
					}
					else if( !strcmp(Parameter,"TEXT") ) {
						PORT->MODE = USTATUS_TEXT;
					}

					break;

		case	OPER_STATUS_DUAL_Q:

					switch(SYSTEM.OPERATION.STATUS){
						case STATUS_ACTIVE:			strcpy(message,"ACTIVE");	break;
						case STATUS_STANDBY:		strcpy(message,"STANDBY");	break;
						case STATUS_PRE_STANDBY:	strcpy(message,"PRE_STANDBY");	break;
						case STATUS_BLOCK:			strcpy(message,"BLOCK");	break;
						case STATUS_UNKNOWN:		strcpy(message,"UNKNOWN");	break;
					}

					
					switch(CounterModule.STATUS){
						case NOT_EXIST:			strcpy(string,"/NONE");		break;
						case STATUS_ACTIVE:		strcpy(string,"/ACTIVE");	break;
						case STATUS_STANDBY:	strcpy(string,"/STANDBY");	break;
						case STATUS_PRE_STANDBY:strcpy(string,"/PRE_STANDBY");	break;
						case STATUS_BLOCK:		strcpy(string,"/BLOCK");	break;
						case STATUS_UNKNOWN:	strcpy(string,"/UNKNOWN");	break;
					}

					strcat(message,string);
					//sprintf(string,"[%2d %2d %2d]",SYSTEM.OPERATION.STATUS,CounterModule.EXIST,CounterModule.STATUS);
					//strcat(message,string);
					break;

		case	OPER_STATUS_SYNC_Q:
                    if( SYSTEM.OPERATION.OPMODE != OPMODE_OPERATION){
                        strcpy(message,"WARMUP");
                    } else {
                        switch(SYSTEM.OPERATION.LOCKMODE){
                            case    LOCKED:
                                    		strcpy(message,"LOCKED");
                                            break;
							case    HOLDOVER:
                                            strcpy(message,"HOLDOVER");
                                            break;
							case	RECOVERYfromUNLOCKED:
                                            strcpy(message,"RECOVERY");
                                            break;
                        }
                    }
					break;
		case	OPER_ERROR_Q:

					sprintf(message,
						"SYSTEM:%d, EB:%d,\r\nOSC:%d 10MHz:%d(c:%d) OCXO:%d(c:%d)\r\nPOWER:%d\r\nEFC:%d RTC:%d\r\ndDAC:%d Rb.:%d",
						DIAG.SYSTEM,DIAG.EB,DIAG.OSC,DIAG._10MHz,DIAG._10MHz_GoodCounter,DIAG.OCXO,DIAG.Watch1PPSCounter,
						DIAG.POWER,DIAG.EFC,DIAG.RTC,DIAG.dDAC,DIAG.RB);
					break;
		case	OPER_LOG_CLEAR_:// ClearLOG
					PresetLOG();
					strcpy(message,"LOG cleared");
					break;
		case	OPER_LOG_COUNT_Q:// Log Count?
					sprintf(message,"%d",LOG.LogCounter);
					break;
		case	OPER_LOG_TEST_P:
					if( sscanf(Parameter,"%d",&t1) == 1 && t1 >= 1 ){

						for ( i = 0; i < t1; i++ ) {
							LogIt(LOG_CATA1_NORM, LOG_CATA2_NOTHING, LOG_DEBUG,i);
							usleep(1000);
						}
					}
					break;
		case	OPER_LOG_Q:// OPER/LOG?
		case	OPER_LOG_ALL_Q:// ALL?

					if( comm == OPER_LOG_Q){	LOG.Page_Mode = ON;}
					else{	LOG.Page_Mode = OFF;}
					LOG.Page_Mode = OFF;

					if( LOG.LogCounter == 0 ) {
						strcpy(message,"No Log message");
					}
					else {
						LOG.DISPLAY_NO = 0;
						LOG.Precise_Mode = OFF;
						LOG.LogFileType = GDU_LOG_FILE_RAM;
						//sprintf(message," %d Log message(s)...",LOG.LogCounter);
						sprintf(MESG, "\n%d Log message(s)...\n",LOG.LogCounter);
						TransmitMessage((BYTE*)MESG,PORT_CONTROL);
						//PORT->MODE = UDISPLAY_LOG; // Command Prompt

						//hmlee - not working!! kind of working but que size is 4kb, so we see only the last 4kb
						//hmlee - need to print out log ourselves
						//sprintf(MESG,"cat %s",NameRAMLogFile);
						//system(MESG);
					}
					break;
		case	OPER_LOG_P: // OPER/LOG 9927 (milli-second)
					//if(!strcmp(Parameter,"9927")) {
					//	if( LOG.LogCounter == 0 ) {
					//		strcpy(message,"No Log message");
					//	}
					//	else {
					//		LOG.DISPLAY_NO = 0;
					//		LOG.Precise_Mode = ON;
					//		sprintf(message," %d Log message(s)...",LOG.LogCounter);
					//		PORT->MODE = UDISPLAY_LOG;
					//	}
					//}
                    //
					//else if(!strcmp(Parameter,"1")) {
                    //
					//	sprintf(message,"LOG> %d %d %d %d",
					//	LOG.FirstLog,
					//	LOG.LastLog,
					//	LOG.LogCounter,
					//	LOG.AccLogCounter);
					//}
					if(!strcmp(Parameter,"NEW")) {
						LOG.LogFileType = GDU_LOG_FILE_NEW;
					}
					else if(!strcmp(Parameter,"OLD")) {
						LOG.LogFileType = GDU_LOG_FILE_OLD;
					}
					else {
						result = EXE_ERROR;
					}


					break;

		case	OPER_TLOG_CLEAR_:// Clear TLOG
					PresetTLOG();
					strcpy(message,"TLOG cleared");
					break;
		case	OPER_TLOG_COUNT_Q:// TLog Count?
					sprintf(message,"%d",TLOG.LogCounter);
					break;
		case	OPER_TLOG_TEST_P:
					if( sscanf(Parameter,"%d",&t1) == 1 && t1 >= 1 ){

						for ( i = 0; i < t1; i++ ) {
							TLogIt(TLOG_Fault,i);
						}
					}
					break;

		case	OPER_TLOG_Q:// OPER/TLOG?
		case	OPER_TLOG_ALL_Q:// ALL?
					if( TLOG.LogCounter == 0 ) {
						strcpy(message,"No Log message");
					}
					else {
						TLOG.DISPLAY_NO = 0;
						//sprintf(message," %d TLog message(s)...",TLOG.LogCounter);
						sprintf(MESG, "%d TLog message(s)...\r\n",TLOG.LogCounter);
						TransmitMessage((BYTE*)MESG,PORT_CONTROL);
						//strcpy(message,"Log Message...");
						SYSTEM.IO.CTRL_PORT.MODE = UDISPLAY_TLOG; // Command Prompt
					}
					break;

		case	OPER_MODE_P:
					if(Parameter[0] == '1')  PORT->MODE = UTOD;
					else if(!strcmp(Parameter,"9927")) PORT->MODE = UDEBUG;
					else if(!strcmp(Parameter,"DEBUG")) {
						PORT->MODE = UDEBUG_DBG;
						strcpy(message,"x[1Jx[0J");
						message[0] = 0x1B; message[4]=0x1B;
					}
					else if(!strcmp(Parameter,"TIME")) PORT->MODE = UDEBUG_TIME;
					else if(!strcmp(Parameter,"DETAIL1")) PORT->MODE = UDEBUG_DETAIL;
					else if(!strcmp(Parameter,"DETAIL2")) PORT->MODE = UDEBUG_DETAIL2;
					else if(!strcmp(Parameter,"OFFSET")) PORT->MODE = UDEBUG_OFFSET;

					else if(Parameter[0] == '0')  PORT->MODE = UCOMMAND;
					else if(Parameter[0] == '2')  PORT->MODE = UTOD_ASCII;
					else if(Parameter[0] == '3')  PORT->MODE = UDEBUG3;
					else if(Parameter[0] == '4')  PORT->MODE = UDEBUG4;
					else if(Parameter[0] == '5'){
						PORT->MODE = UDEBUG5;
						strcpy(message,"x[1Jx[0J");
						message[0] = 0x1B; message[4]=0x1B;
						break;

					}
					else if(Parameter[0] == '6')  PORT->MODE = UDEBUG6;
					else if(Parameter[0] == '7')  PORT->MODE = UDEBUG7;
					else if(Parameter[0] == '8')  Init_QuickStart();
					else if(Parameter[0] == '9')  PORT->MODE = UDEBUG9;


					else if(Parameter[0] == 'D')  PORT->MODE = UDEBUGD;
					//else if(Parameter[0] == '6')  PORT->MODE = UDEBUG2;

                    break;

		case	DETAIL_:
					for(i = 0; i < MAX_DETAIL_TABLE;i++){
						sprintf(MESG,"%d,",ssDetailTable[i]);
						TransmitMessage((BYTE*)MESG,PORT_CONTROL);
					}
					break;
/*
        case	111: // Prompt d
        			// ���?OD1 d�?���?
					if( !strcmp(Parameter,"ON") ){
						SYSTEM.IO.TOD_PORT.PROMPT = ON;
					}
					else if( !strcmp(Parameter,"OFF") ){
						SYSTEM.IO.TOD_PORT.PROMPT = OFF;
        			}
					break;
*/
        case	OPER_DUAL_ACTIVE_P:
        			if (SYSTEM.OPERATION.STATUS != STATUS_ACTIVE){
        				sprintf(message,"Command only avaible for Active GRCA");
        			}
					else if( !strcmp(Parameter,"A") ){

						switch ( SYSTEM.ID ) {
							case ModuleA:
									if (SYSTEM.OPERATION.STATUS == STATUS_STANDBY  && CounterModule.STATUS == STATUS_ACTIVE ){
										MyModule.ForceBlock = ON;
										MyModule.Command_ForceBlock = ON;
									}
									break;

							case ModuleB:
									if (SYSTEM.OPERATION.STATUS == STATUS_ACTIVE  && CounterModule.STATUS == STATUS_STANDBY ){
										MyModule.SelfBlock = ON;
										MyModule.Command_SelfBlock = ON;
									}
									break;
						}
					}
					else if( !strcmp(Parameter,"B") ){
						switch ( SYSTEM.ID ) {
							case ModuleB:
									if (SYSTEM.OPERATION.STATUS == STATUS_STANDBY  && CounterModule.STATUS == STATUS_ACTIVE ){
										MyModule.ForceBlock = ON;
										MyModule.Command_ForceBlock = ON;
									}
									break;

							case ModuleA:
									if (SYSTEM.OPERATION.STATUS == STATUS_ACTIVE  && CounterModule.STATUS == STATUS_STANDBY ){
										MyModule.SelfBlock = ON;
										MyModule.Command_SelfBlock = ON;
									}
									break;
						}
        			}
					else {
						result = EXE_ERROR;
					}
					
					break;

//----------------------------------------------------------
//	GPS
//----------------------------------------------------------
		case	GPS_TIME_Q:// Time?
                    sprintf(message,"%d:%d:%d",SYSTEM.TIME.CUR.HOUR,SYSTEM.TIME.CUR.MINUTE,SYSTEM.TIME.CUR.SECOND);
					break;

		case	GPS_TIME_P:
					// Set Time
/*
					if( sscanf(Parameter,"%d,%d,%d",&t1,&t2,&t3) == 3){
						if ( t1 >= 0 && t1 <= 23 && t2 >= 0 && t2 <= 59 && t3 >= 0 && t3 <= 59 ) {

							//SetTIME((unsigned char)t1,(unsigned char)t2,(unsigned char)t3);
						}
					}
					else {
						result = EXE_ERROR;
					}
*/					
					break;

		case	GPS_DATE_Q:// Date?
					sprintf(message,"%d/%d/%d",SYSTEM.TIME.CUR.YEAR,SYSTEM.TIME.CUR.MONTH,SYSTEM.TIME.CUR.DAY);
					break;

		case	GPS_DATE_P:
					// Set Date
/*					
					if( sscanf(Parameter,"%d,%d,%d",&t1,&t2,&t3) == 3){
						if ( t1 > 0 && t2 > 0 && t2 <= 12 && t3 > 0 && t3 <= 31 ) {
							//SetDATE((unsigned short)t1,(unsigned char)t2,(unsigned char)t3);
						}
					}
					else {
						result = EXE_ERROR;
					}
*/					
					break;

		case	GPS_TRACK_Q:
					break;
		case	GPS_TRACK_DOP_Q:
					sprintf(message,"%3.1f",SYSTEM.TRACK.DOP/10.0);
					break;

		case	GPS_TRACK_MASK_Q:
					sprintf(message,"%d",SYSTEM.TRACK.MASK);
					break;

		case	GPS_TRACK_MASK_P:
				    if( sscanf(Parameter,"%d",&t1) == 1){
				    	if( t1 >= 0 && t1 <= 89 ){
				    		CONFIG.MASK = (char)t1;
							SetMASKANGLE(CONFIG.MASK);
							CONFIG.WriteEvent = ON;

							//sprintf(message,"%d",CONFIG.MASK);
				    	}
				    }
				    break;

		case	GPS_TRACK_SAT_Q:// Tracking Sat?
                    for(i = 0, t1 = 0; i < 12; i++){
                        if( SYSTEM.TRACK.SAT[i].SS > 0 ){
                            sprintf(string,"%d,",SYSTEM.TRACK.SAT[i].ID);	strcat(message,string);
                            t1++;
                        }
                    }
                    if( t1 > 0 )	message[strlen(message)-1] = 0;
                    else   			strcpy(message,"0");

					break;

		case	GPS_TRACK_SAT_COUNT_Q:// Tracking Sat num?
                    for(i = 0, t1 = 0; i < 12; i++){	if( SYSTEM.TRACK.SAT[i].SS > 0 )  t1++;	}
                    sprintf(message,"%d",t1);
					break;


		case	GPS_TRACK_COUNTER_CLEAR_:
					break;

		case	GPS_POS_MODE_Q:// Position Mode?
					switch(SYSTEM.POSITION.PositionMode){
						case POSITION_MODE_KNOWN:		strcpy(message,"Known");	break;
						case POSITION_MODE_AVERAGING:	strcpy(message,"Averaging");	break;
						case POSITION_MODE_DYNAMIC:		strcpy(message,"Dynamic");	break;
//						case POSITION_MODE_UNKNOWN:		strcpy(message,"uNknown");	break;
					}
					break;
/*
		case	GPS_POS_MODE_P:
					if(!strcmp(Parameter,"AVG") ){
						if( SYSTEM.POSITION.PositionMode == POSITION_MODE_KNOWN){
							SYSTEM.EB.PHM_Disable = ON;
						}

						Start_Position_Survey(0);
						CONFIG.PositionMode = POSITION_MODE_AVERAGING;
                       	CONFIG.WriteEvent = ON;	// Positioin Mode  ٲ�?Ƿ�⼭ config ���?

					}
					else if(!strcmp(Parameter,"DYNAMIC") ){
						if( SYSTEM.POSITION.PositionMode == POSITION_MODE_KNOWN){
							SYSTEM.EB.PHM_Disable = ON;
						}
						SYSTEM.POSITION.PositionMode = POSITION_MODE_DYNAMIC;
						CONFIG.PositionMode = POSITION_MODE_DYNAMIC;
                       	CONFIG.WriteEvent = ON;	// Positioin Mode  ٲ�?Ƿ�⼭ config ���?
					}
					break;
*/

		case	GPS_POS_FIX_Q:
					//if(	CONFIG.PositionMode == ON) {
					//	strcpy(message,"ON");
					//}
					//else {
					//	strcpy(message,"OFF");
					//}
					strcpy(message,GetPositionString(SYSTEM.EB.PHP.POSITION));
					
					break;
/*					
		case	GPS_POS_FIX_P:
					if( !strcmp(Parameter,"ON")){
						CONFIG.POSITION_FIX = ON;
						strcpy(message,"ON");
						CONFIG.WriteEvent = ON;
					} else if (!strcmp(Parameter,"OFF")){
						CONFIG.POSITION_FIX = OFF;
						strcpy(message,"OFF");
						CONFIG.WriteEvent = ON;
					}
					else {
						result = EXE_ERROR;
					}
					break;
*/
		case	GPS_POS_CURRENT_Q:// Current Position?

					strcpy(message,GetPositionString(SYSTEM.POSITION.CURRENT));
					break;

		case	GPS_POS_AVERAGE_Q:// Average Position?

					strcpy(message,GetPositionString(SYSTEM.POSITION.AVERAGE));
					break;

		case	GPS_POS_AVERAGE_START_:// Start Average

					if (( SYSTEM.OPERATION.OPMODE == OPMODE_OPERATION ) ||
						(( SYSTEM.OPERATION.OPMODE == OPMODE_WARMUP ) && (SYSTEM.EB.PHM == DISABLE) ) ){

						Start_Position_Survey(0);
						if ( SYSTEM.EB.PHM == ENABLE ){
							SYSTEM.EB.PHM_Disable = ON;
						}
					}
					//if( SYSTEM.POSITION.Survey == SURVEY_END) {
					//	SYSTEM.POSITION.Survey = SURVEY_ON;
					//}
					break;
		case	GPS_POS_AVERAGE_END_:// End Average -> �����?
					break;
		case	GPS_POS_LAST_Q:// Last hold position?
					strcpy(message,GetPositionString(SYSTEM.POSITION.LASTHELD));
					break;

		case	GPS_POS_HOLD_P:// hold position
					if( (SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP) || ( SYSTEM.EB.PHP_Enable == ON ) ||
						(SYSTEM.OPERATION.OPMODE == OPMODE_WARMUP && SYSTEM.EB.PHM == ENABLE ) )

						{	break;	}

					if( !strcmp(Parameter,"LAST") &&
						(SYSTEM.POSITION.LASTHELD.LAT != 0 && SYSTEM.POSITION.LASTHELD.LON != 0 && SYSTEM.POSITION.LASTHELD.HEI != 0)){

						SYSTEM.EB.PHP.POSITION = SYSTEM.POSITION.LASTHELD;
						SYSTEM.EB.PHP_Enable = ON;
						//SYSTEM.POSITION.Survey = SURVEY_END;

					}
					else if(!strcmp(Parameter,"AVERAGE") &&
						(SYSTEM.POSITION.AVERAGE.LAT != 0  && SYSTEM.POSITION.AVERAGE.LON != 0 && SYSTEM.POSITION.AVERAGE.HEI != 0)){

						SYSTEM.EB.PHP.POSITION = SYSTEM.POSITION.AVERAGE;
						SYSTEM.EB.PHP_Enable = ON;
						//SYSTEM.POSITION.LASTHELD = PHP.POSITION;
              			//CONFIG.POSITION = SYSTEM.POSITION.AVERAGE;

						//SYSTEM.POSITION.Survey = SURVEY_END;

						//CONFIG.WriteEvent = ON;
					}
					else if( sscanf(Parameter,"%c,%d,%d,%f,%c,%d,%d,%f,%f",&d1,&t1,&t2,&f1,&d2,&l1,&l2,&f2,&f3) == 9){

							// use current input position

						k1 = (long)t1*3600000+(long)t2*60000+(long)(f1*1000.0);
						k1 = ( (d1 == 'N') || (d1 == 'n')) ? k1:-k1;
						k2 = (long)l1*3600000+(long)l2*60000+(long)(f2*1000.0);
						k2 = ( (d2 == 'E') || (d2 == 'e')) ? k2:-k2;
						k3 = (long)(f3*100.0);

						SYSTEM.EB.PHP.POSITION.LAT = k1;
						SYSTEM.EB.PHP.POSITION.LON = k2;
						SYSTEM.EB.PHP.POSITION.HEI = k3;

						//SYSTEM.POSITION.LASTHELD = PHP.POSITION;
                        //CONFIG.POSITION = SYSTEM.POSITION.AVERAGE;
						SYSTEM.EB.PHP_Enable = ON;
						//SYSTEM.POSITION.Survey = SURVEY_END;

						//CONFIG.WriteEvent = ON;

					}
					else {
						result = EXE_ERROR;
					}
					break;

//----------------------------------------------------------
//	TIME
//----------------------------------------------------------
		case	TIME_RTC_P:
					break;

		case	TIME_ZONE_P:
					if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){
						result = EXE_ERROR;
						break;
					}
					// Set time Zone ...
                    if ( strchr(Parameter,',') &&
						( sscanf(Parameter,"%d,%d",&t1,&t2) == 2 && t1 >= -12 && t1 <= 12 && t2 >= 0 && t2 <= 59 )){

                        SYSTEM.TIME.LOCALHOUR = (char)t1;
                        SYSTEM.TIME.LOCALMINUTE = (char)t2;

                        CONFIG.LOCALHOUR = SYSTEM.TIME.LOCALHOUR;
                        CONFIG.LOCALMINUTE = SYSTEM.TIME.LOCALMINUTE;
                        CONFIG.WriteEvent = ON;

	                    sprintf(message,"%d,%d",CONFIG.LOCALHOUR,CONFIG.LOCALMINUTE);
                    }
                    else if( sscanf(Parameter,"%d",&t1) == 1 && t1 >= -12 && t1 <= 12 ){
                        SYSTEM.TIME.LOCALHOUR = (char)t1;
                        CONFIG.LOCALHOUR = SYSTEM.TIME.LOCALHOUR;
						CONFIG.WriteEvent = ON;

	                    sprintf(message,"%d,%d",CONFIG.LOCALHOUR,CONFIG.LOCALMINUTE);
                    }
                    else {
	                    result = EXE_ERROR;
                    }
					break;

		case	TIME_ZONE_Q:
					if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}
					// Time zone?
                    sprintf(message,"%d,%d",CONFIG.LOCALHOUR,CONFIG.LOCALMINUTE);
					break;
		case	TIME_MODE_P:
					if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}
					// Set Time Mode
					if( !strcmp(Parameter,"GPS") ) {

						SYSTEM.TIME.TIMEMODE = TIMEMODE_GPS;
						CONFIG.TimeMode = TIMEMODE_GPS;
						CONFIG.WriteEvent = ON;

						strcpy(message,"GPS");

					} else if( !strcmp(Parameter,"UTC") ) {

						SYSTEM.TIME.TIMEMODE = TIMEMODE_UTC;
						CONFIG.TimeMode = TIMEMODE_UTC;
						CONFIG.WriteEvent = ON;

						strcpy(message,"UTC");
					}
					else {
						result = EXE_ERROR;
					}
					break;

		case	TIME_MODE_Q:
					if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}
					// Time Mode?
					switch(SYSTEM.TIME.TIMEMODE){
						case TIMEMODE_GPS:	strcpy(message,"GPS");	break;
						case TIMEMODE_UTC:	strcpy(message,"UTC");	break;
					}
					break;


		case	TIME_LEAPSECOND_Q:
					if( SYSTEM.EB.STATUS != EB_STATUS_RUN){	break;	}
                    sprintf(message,"%+d",SYSTEM.TIME.LEAPSECOND);
					break;

		case	TIME_ADELAY_P:
					if( SYSTEM.EB.STATUS != EB_STATUS_RUN){	break;	}
					// Set Antenna Delay
                    if( sscanf(Parameter,"%ld",&k1) == 1){

						if( SYSTEM.OPERATION.STATUS == STATUS_ACTIVE ){

		                    k2 = k1 - CONFIG.ANTDLY;
    		                if( k2 > 0 ){      	k2 += 50;    	}
							else {				k2 -= 50;		}

							RECOVERY.flag = ON;
							RECOVERY.wait_time = 10;
							RECOVERY.average_time = 20;
							RcvAlarm.skip_counter = 30;
							RECOVERY.IntervalCounter = 0;
						}

                        CONFIG.ANTDLY = k1;
						SetANTDLY(k1);
                        CONFIG.WriteEvent = ON;
    	                sprintf(message,"%ld->%ld",SYSTEM.TIME.ANTDLY,CONFIG.ANTDLY);
					}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	TIME_ADELAY_Q:
					//if( SYSTEM.EB.STATUS != EB_STATUS_RUN){	break;	}
					// Antenna Delay?
                    //sprintf(message,"%ld",SYSTEM.TIME.ANTDLY);
                    sprintf(message,"%ld",CONFIG.ANTDLY);
					break;

		case	TIME_CODE_Q:
					if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}
					// Time Code ?
					sprintf(message,"%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X",
							TOD.BUFFER[0],TOD.BUFFER[1],TOD.BUFFER[2],TOD.BUFFER[3],TOD.BUFFER[4],
							TOD.BUFFER[5],TOD.BUFFER[6],TOD.BUFFER[7],TOD.BUFFER[8],TOD.BUFFER[9],
							TOD.BUFFER[10],TOD.BUFFER[11],TOD.BUFFER[12],TOD.BUFFER[13],TOD.BUFFER[14],
							TOD.BUFFER[15],TOD.BUFFER[16],TOD.BUFFER[17],TOD.BUFFER[18],TOD.BUFFER[19]);
					break;

		case	TIME_CODE_P: // Time Code continuous
                   	if(sscanf(Parameter,"%d",&t1) == 1){
                   		if( t1 == 99 ) {
                   			PORT->MODE = UTOD_ASCII;
                   		}
                   	}
					break;

//----------------------------------------------------------
//	SYSTEM
//----------------------------------------------------------
/*
		case	SYSTEM_SELFTEST_:// self test

//					if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}


					S_ADDR = ADDRESS_INFORMATION;      //
					s1 = S_ADDR[12];

			   		//Disable
   					asm ("  move.w      #$2700,SR");


					S_ADDR = ADDRESS_PROGRAM;      //
       				s2 = CheckCRC16((unsigned char *)S_ADDR,0x3A000);

		   			//Enable
		   			asm ("  move.w      #$2300,SR");

					sprintf(message,"value: %.4X result: %.4X\r\n",s1,s2);
					break;

		case	SYSTEM_SELFTEST_Q:
					S_ADDR = ADDRESS_PROGRAM;
					for( i = 0; i < 0x1D0 ; i++ ){
						s2 = CheckCRC16((unsigned char*)S_ADDR,0x200);

			        	sprintf((char*)MESG,"[%.6X] %.4X\r\n",s2,S_ADDR);
			        	TransmitMessage((BYTE*)MESG,DEBUG_PORT);
						S_ADDR += 0x100;
					}
					break;
*/
		case	SYSTEM_RESET_P:// system reset

					if( !strcmp(Parameter,"CONFIG")){
						PresetCONFIG();
						strcpy(message,"CONFIG cleared");
					} else if (!strcmp(Parameter,"SYSTEM")){
						PresetCONFIG();
				        //RequestEB(eDEFAULT);
						strcpy(message,"SYSTEM cleared");
					} else if (!strcmp(Parameter,"ALMANAC")){
						//2005-01-25 9:29�?
				        RequestEB(eDEFAULT);
						//strcpy(message,"ALMANAC cleared");

						gwait(500);
						if(SYSTEM.OPERATION.STATUS == STATUS_ACTIVE){
       						SetMeToSelfBlock();
      					}
      					reset_sub_call();
      					SystemReset();
      					reset_system();
					}
					else if (!strcmp(Parameter,"DEFAULT,MTI1")){
						
						if(IsRbModel()){
							strcpy(message,"Not Applied!!! OSC is Rb.");
						}
						else{
							PresetCONFIG();

							CONFIG.SectorWriteCounter = 0;
													
							CONFIG.GAIN	=	1.4E-10;
							CONFIG.DAC	= 	17000;
							CONFIG.OCXO_ID[0] = 0;
							
							CONFIG.WriteEvent = ON;
							
							LOG.AccLogCounter = 0;
							LOG.LogPresetEvent = ON;
							
							strcpy(message,"System Initializied for MTI1");
						}
					}
					else if (!strcmp(Parameter,"DEFAULT,MTI2")){

		  				if(IsRbModel()){
							strcpy(message,"Not Applied!!! OSC is Rb.");
						}
						else{
							PresetCONFIG();

							CONFIG.SectorWriteCounter = 0;
													
							CONFIG.GAIN	=	8.5E-11;//MTI1�?Gain�?��
							CONFIG.DAC	= 	32767;
							CONFIG.OCXO_ID[0] = 0;
							
							CONFIG.WriteEvent = ON;
							
							LOG.AccLogCounter = 0;
							LOG.LogPresetEvent = ON;
							
							strcpy(message,"System Initializied for MTI2");
						}
					}
					else if (!strcmp(Parameter,"DEFAULT")){

		  				PresetCONFIG();

		  				CONFIG.SectorWriteCounter = 0;

						if( 0 &&IsRbModel()){
							CONFIG.GAIN	=	1.0E-11;
							CONFIG.DAC	= 	0;
							CONFIG.OCXO_ID[0] = 0;
						}
						else {
							CONFIG.GAIN	=	1.24E-11;
							CONFIG.DAC	= 	32767;
							CONFIG.OCXO_ID[0] = 0;
						}

	    				CONFIG.WriteEvent = ON;

    	    			LOG.AccLogCounter = 0;
    	    			LOG.LogPresetEvent = ON;

						strcpy(message,"System Initializied");
					}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	SYSTEM_RESET_:// RESET
					reset_sub_call();
					break;



		case	SYSTEM_COMM_Q:// Comm port?
/*			
					strcpy(message,"PORT ");
					if( port == PORT_CONTROL )
						message[4] = '3';
					else if (port == PORT_COMMON)
						message[4] = '1';
*/
					break;

		case	SYSTEM_COMM_BAUD_Q:// CONTROL Baud Rate?
					//strcpy(message,GetBaudrateString(PORT_CONTROL));
					strcpy(message,BaudrateString[CONFIG.SCC3.Baudrate]);
					break;

		case	SYSTEM_COMM_BAUD_P:// Set CONTROL baud rate
					d1 = CONFIG.SCC3.Baudrate;
					d2 = GetBaudrate(Parameter);

					if(d2 != -1){

						if( d1 != d2 ){
	       					ChangeBaudRate(PORT_CONTROL,d2);
							CONFIG.SCC3.Baudrate = d2;
	       					CONFIG.WriteEvent = ON;

							strcpy(message,BaudrateString[CONFIG.SCC3.Baudrate]);
						}
					}
					else{
						strcpy(message,"Not valid baud rate");
					}
					break;

		case    SYSTEM_FIRMWARE_VERSION_Q:
						/* Firmwar Version? */

					strcpy(message,FirmWare_Version);
					break;

		case    FUPGRADEM_:    /* Download */
					//kang ---> Down.c	Start_DOWNLOAD(DOWNLOAD_MODE_PROGRAM);
					break;

		case    FUPGRADEL_:    /* Download */
					//kang ---> Down.c	Start_DOWNLOAD(DOWNLOAD_MODE_LOADER);
					break;

		case    FUPGRADET_:    /* Download */
					CONFIG.WriteEvent = ON;
					//kang ---> Down.c	Start_DOWNLOAD(DOWNLOAD_MODE_TID);
					break;
		
		case	FUPGRADEG_:

					//LogIt3(LOG_SYNC15_UPGRADE,1,0,0);

					break;

		case	SYSTEM_CONFIG_Q:
					Show_Config();
					break;

		case	SYSTEM_SN_Q: // Hardware version
					strcpy(message,HardWare_Version);
					break;

		case	SYSTEM_SN_P:
                    if( sscanf(Parameter,"%d",&t1) == 1){

						if ( t1 > 0 && t1 < 99999999 ) {

							//S_ADDR = ADDRESS_INFORMATION;
							//S_ADDR[13] = t1;
							CONFIG.VERSION = t1;
							sprintf(HardWare_Version,HardWareName"%.8d",t1);

							strcpy(message,HardWare_Version);

							CONFIG.WriteEvent = ON;
						}
	                    else{
    	                	result = EXE_ERROR;
        	            }
					}
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	SYSTEM_REVISION_Q:
					sprintf(message,"%d",CONFIG.HWrevision);
					break;
		
		case	SYSTEM_REVISION_P:
					if( sscanf(Parameter,"%d",&t1) == 1){

						if ( (t1 >= cfConfig[cHWrevision].min) && (t1 <= cfConfig[cHWrevision].max) ) {
							CONFIG.HWrevision = t1;
							sprintf(message,"%d",CONFIG.HWrevision);

							CONFIG.WriteEvent = ON;
						}
	                    else{
    	                	result = EXE_ERROR;
        	            }
					}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
					
		case	SYSTEM_INFO_Q:	// Information Sector
					GetIPconfig(ip_cfg_MAC ,dummy);
					if(CompleteTestRomRamFlag == YES){
	    				TransmitMessage((unsigned char*)"\r\n====================================================\r\n",port);
	    				//S_ADDR = (unsigned short*)(ADDRESS_PROGRAM_INFORMATION+ROM_BASE_ADDRESS);
	    				sprintf((char*)MESG,
	    								"S/N       : %s\r\n"
	    								Product"     : %s\r\n"
	    								"Build     : "Build_date" "Build_time"\r\n"
	    								"FPGA Ver  : %s\r\n"
	    								"OCXO ID   : %s\r\n"
	    								"DAC       : %d\r\n"
	    								"GAIN      : %E\r\n"
	    								"TGAIN     : %E\r\n"
	    								"MAC       : %s\r\n"
	    								,
	    								//HardWare_Version,
	    								GetSerialNumber(),
	    								FirmWare_Version,
	    								//S_ADDR[11],
	    								//S_ADDR[5],S_ADDR[6],S_ADDR[7],S_ADDR[8],S_ADDR[9],S_ADDR[10],
	    								FPGA_Version,
	    								CONFIG.OCXO_ID,
	    								CONFIG.DAC,
	    								CONFIG.GAIN,
	    								CONFIG.iTemperatureGain/100.,
	    								mac_str
	    								);
	   					TransmitMessage((unsigned char*)MESG,port);
	
	   					//if(OCXO_ID.Valid == YES) {
	   					//	//kang if(id_mode == ID_MODE_FREQ) {sprintf(MESG,"Temp ID   : Valid(TID)");}
						//	if(id_mode == ID_MODE_DAC) {sprintf(MESG,"Temp ID   : Valid(TID)");}
	   					//	else if(id_mode == ID_MODE_CURRENT) {sprintf(MESG,"Temp ID   : Valid(CID)");}
						//}
						//else {
						//	sprintf(MESG,"Temp ID   : Invalid");
						//}
	                    //
						//TransmitMessage((unsigned char*)MESG,port);
	
			 			str_model = " ";
			 			str_manu = " ";
			 			str_date = " ";
			 			memset(str_ver,0,sizeof(str_ver));
			 			
			 			if(SYSTEM.EB.ID != UNDEFINED){
			 				if(SYSTEM.EB.SUBID == SYNC15_ORI){
			 					str_model = "SYNC-15";
			 				}
			 				else if(SYSTEM.EB.SUBID == SYNC15_N){
			 					str_model = "SYNC-15N";
			 				}
			 				else if(SYSTEM.EB.SUBID == SYNC15_T){
			 					str_model = "SYNC-15T";
			 				}
			 				
			 				str_date = SYSTEM.EB.SWDATE;
			 				sprintf(str_ver,"%s.%s", 
			 					SYSTEM.EB.SWVER,SYSTEM.EB.SWREV);
			 				
			 				if(SYSTEM.EB.ID == SYNC_15){
			 					str_manu = "NAVICOM";
			 				}else{
			 					str_manu = "MOTOROLA";
			 				}
			 			}
			 			
//						sprintf((char*)MESG,
//	    								"RCV Model : %s\r\n"
//	    								"RCV Man   : %s\r\n"
//	    								"RCV Ver   : %s\r\n"
//	    								"RCV Date  : %s",
//	    								str_model,
//	    								str_manu,
//	    								str_ver,
//	    								str_date
//	    								);
//	   					TransmitMessage((unsigned char*)MESG,port);
									
	   					strcpy(message,"====================================================\r\n");
	   				}
	   				else{
	   					strcpy(message,"Hasn't completed the ROM/RAM test.\r\n");
	   					if(GC.RunningTime > 60){
	   						strcat(message,"Check OSC!!!\r\n");
	   					}
	   				}	   				
					break;


		case	SYSTEM_INFO_P:	// CONFIG, LOG Information
					if( !strcmp(Parameter,"9927")){

						sprintf(message,"# of Config: %d\r\n# of Log: %d",
							CONFIG.SectorWriteCounter,LOG.AccLogCounter);
					}
					break;

		case	SYSTEM_VERSION_Q:
					//fwup_show_version();
   					//TransmitMessage((unsigned char*)"\r\n====================================================\r\n",port);
					//send_version();
   					//strcpy(message,"\r\n====================================================\r\n");
					break;
//----------------------------------------------------------
//	Development Command
//----------------------------------------------------------


		case	DEV_SIG_P:
					if( !strcmp(Parameter,"ON")){
						if(1 || USER.MANUAL_CONTROL == ON){
							UnconditionalSigOn();
							strcpy(message,"ON");
						}
					} else if (!strcmp(Parameter,"OFF")){
						if(1 || USER.MANUAL_CONTROL == ON){
							UnconditionalSigOff();
							strcpy(message,"OFF");
						}
					}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	DEV_SIG_Q:
					if( IsSigOn() ){
						strcpy(message,"ON");
					}
					else {
						strcpy(message,"OFF");
					}
					break;
		case	DEV_MEM_Q:
					for(i = 0; i < 8; i++ ){
						//sprintf(MESG,"%.8x\r\n",Section_Address[i]);
						//TransmitMessage((BYTE*)MESG,PORT_CONTROL);
					}
					break;
		case	DEV_OFFSET_P:
					if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}
					
                    if( sscanf(Parameter,"%ld",&k1) == 1){
                        Set1PPSoffset(k1);
					}
	                    
    	            sprintf(message,"%ld -> %ld",offset_1pps, k1);
    	            offset_1pps = k1;

					break;
		case	DEV_OFFSET_Q:
					if( SYSTEM.OPERATION.OPMODE == OPMODE_POWERUP){	break;	}
					// Antenna Delay?
                    sprintf(message,"%ld",offset_1pps);
					break;
		case	MEM_READ_P:
					if((sscanf(Parameter,"%ld",&k1) == 1) && (k1 >= 0)){
						t1 = GetFpgaValue(k1);
						sprintf(message,"get fpga %ld: %d",k1, t1);
					}
					else{
						result = EXE_ERROR;
					}
					break;

		case	MEM_WRITE_P:
                    if((sscanf(Parameter,"%ld %d",&k1,&t1) == 2) && (k1 >= 0 && t1 >=0)){
						SetFpgaValue( k1, (t1&0xff) );
						sprintf(message,"set fpga %ld: %d",k1, t1);
					}
					else{
						result = EXE_ERROR;
					}
					break;
        case    PWM_P:
                    if(sscanf(Parameter,"%d",&t1) == 1){
                   		sprintf(message,"PWM DAC:%d\r\n",t1);
                        SetDACPWM((unsigned) t1);
                    }
                    
                    break;
		case	DEV_LSI_P:
					
					if( !strcmp(Parameter,"ON")){
						bLsiMonitorEnable = ON ;
					}
					else if( !strcmp(Parameter,"OFF")){
						bLsiMonitorEnable = OFF;
					}

                    if(sscanf(Parameter,"%d",&t1) == 1){
                    	iLsiDebugVariable= t1 ;

                    	if(iLsiDebugVariable == 3000){
                    		CurDetailIndex = 0;
                    	}

                    }
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	DEV_CONFIG_DAC_P:	// Set CONFIG.DAC
                   	if(sscanf(Parameter,"%d",&t1) == 1){
						CONFIG.DAC = (int)t1;
						CONFIG.WriteEvent = ON;
                   		sprintf(message,"DAC:%d",CONFIG.DAC);
                   	}
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	DEV_CONFIG_DAC_Q:
					sprintf(message,"%d",CONFIG.DAC);
					break;

		case	DEV_CONFIG_GAIN_P:	// Set CONFIG.GAIN
                   	if(sscanf(Parameter,"%f",&f1) == 1){
						CONFIG.GAIN = f1*0.00000000001;
						CONFIG.WriteEvent = ON;
                   		sprintf(message,"%E",CONFIG.GAIN);
                   	}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	DEV_CONFIG_GAIN_Q:
                   	//sprintf(message,"%15.13f",CONFIG.GAIN);
                   	sprintf(message,"%E",CONFIG.GAIN);
					break;

		case	DEV_CONFIG_OCXO_P:
					if(strlen(Parameter) < 12){
						strcpy(CONFIG.OCXO_ID,Parameter);
						CONFIG.WriteEvent = ON;
                   		sprintf(message,"ID:%s",CONFIG.OCXO_ID);
                   	}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	DEV_CONFIG_OCXO_Q:
                   	sprintf(message,"ID:%s",CONFIG.OCXO_ID);
					break;


		case	DEV_CONFIG_DELAY_P:
                   	if(sscanf(Parameter,"%d",&t1) == 1){
						if( t1 >= 0 && t1 <= 255 ){
							CONFIG.delay_value = (unsigned char)t1;
							CONFIG.WriteEvent = ON;
							
    	               		sprintf(message,"Delay:%d",CONFIG.delay_value);
							
							//kang-----------------------------------------------------
							//CounterModule.delay = (unsigned char)t1;
    	               		//sprintf(message,"Delay:%d, Counter Module Delay:%d", CONFIG.delay_value, CounterModule.delay);
							//kang-----------------------------------------------------
							
						}
                   	}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	DEV_CONFIG_DELAY_Q:
                   	sprintf(message,"Delay:%d",CONFIG.delay_value);
					
					//kang-----------------------------------
					//sprintf(message,"Delay:%d, Counter Module Delay:%d", CONFIG.delay_value, CounterModule.delay);
					//kang-----------------------------------

					break;


		case	DEV_CONFIG_E1_ACC_P:
/*			
					if(sscanf(Parameter,"%f",&f1) == 1){				
						CONFIG.E1_Accuracy = f1;
						CONFIG.WriteEvent = ON;
						sprintf(message,"E1 Accuracy:%f[Unit:1E-7]",CONFIG.E1_Accuracy);
						SYSTEM.E1.Accuracy = CONFIG.E1_Accuracy;
						SYSTEM.E2.Accuracy = CONFIG.E1_Accuracy;
					}
					else{
						sprintf(message,"Check!!! Unit:1E-7");
					}
*/					
					break;
		case	DEV_CONFIG_E1_ACC_Q:
/*			
					sprintf(message,"E1 Accuracy:%f [Unit:1E-7]",CONFIG.E1_Accuracy);					
*/					
					break;
		case	DEV_CONFIG_E1_JITTER_P:
/*			
					if(sscanf(Parameter,"%f",&f1) == 1){
						CONFIG.E1_Jitter_Boundary = f1;
						CONFIG.WriteEvent = ON;
						sprintf(message,"E1 Jitter Boundary:%f[Unit:1E-7]",CONFIG.E1_Jitter_Boundary);
						SYSTEM.E1.Jitter_Boundary = CONFIG.E1_Jitter_Boundary;
						SYSTEM.E2.Jitter_Boundary = CONFIG.E1_Jitter_Boundary;
					}
					else{
						sprintf(message,"Check!!! Unit:1E-7");
					}
*/					
					break;
		case	DEV_CONFIG_E1_JITTER_Q:
/*			
					sprintf(message,"E1 Jitter Boundary:%f[Unit:1E-7]",CONFIG.E1_Jitter_Boundary);
*/					
					break;


		case	DEV_TEST_Q:

                    	//sprintf(message,"Test:0x%x==>0x%x\r\n",preFPGAAddr,ReadFPGA(preFPGAAddr));
                    	//sprintf(message,"Test:0x%x==>0x%x\r\n",preFPGAAddr,ReadFPGA(preFPGAAddr));
                    	//sprintf(message,"Test:0x%x==>0x%x\r\n",preFPGAAddr,ReadFPGAArray(preFPGAAddr));
                    	sprintf(message,"Kp:%f,Ki:%f\r\n",Kp,Ki);
                                
					break;

		case	DEV_TEST_P:
					
                    if(sscanf(Parameter,"%f,%f",&f1,&f2) == 2){
                    	Kp=f1;
                    	Ki=f2;
                        //WirteFPGA1( 7,t1 );
                        //preFPGAAddr = t1;
                        //WriteFPGA1( t1,t2 );
                    	sprintf(message,"Kp:%f,Ki:%f\r\n",Kp,Ki);
                    	//sprintf(message,"Test:0x%x,0x%x\r\n",t1,t2);
                    }


					break;




//----------------------------------------------------------
//	Control(Debug) Command
//----------------------------------------------------------
		case	CONTROL_DAC_P:
                    if(USER.MANUAL_CONTROL == ON){
                    	if(sscanf(Parameter,"%d",&t1) == 1){

							if( CGM_P() ){
								CONTROL.ControlDAC = CONTROL.RealDAC = (double)t1;
								//SetRbFreq();
					  			RbSRO_Control();	//sewon 120503
        	            		sprintf(message,"DAC:%d",(int)CONTROL.RealDAC);
							}
							else {

								if( t1 >= 0 && t1 <= 65535 ){
									CONTROL.ControlDAC = CONTROL.RealDAC = (double)t1;
				                	//MakePWMInterval(CONTROL.RealDAC);
	    							CONTROL.CurrentDAC = (unsigned short)(CONTROL.RealDAC);
	        	            		sprintf(message,"DAC:%d",CONTROL.CurrentDAC);
	        	            	}
	        	            	else {
									strcpy(message,"Out of Range");
	        	            	}
	                    	}
						}
					} else {
						strcpy(message,"not Manual Mode");
					}
					break;

		case    CONTROL_MANUAL_Q:   // manual control
					if(USER.MANUAL_CONTROL == ON){
						strcpy(message,"ON");
					} else {
						strcpy(message,"OFF");
					}
					break;

		case    CONTROL_MANUAL_P:   // manual control
                    if( !strcmp(Parameter,"ON") ){
						USER.MANUAL_CONTROL = ON;
						SYSTEM.IO.CTRL_PORT.MODE = UDEBUG;
						strcpy(message,"ON");
                    }

                    else if(!strcmp(Parameter,"OFF")){
						USER.MANUAL_CONTROL = OFF;
						strcpy(message,"OFF");
                    }

                    else if(!strcmp(Parameter,"PASS")){
						CONTROL.RealDAC	= CONTROL.NominalDAC;
                    }
                    else{
                    	result = EXE_ERROR;
                    }
					break;


		case    CONTROL_TGAIN_Q:

                    sprintf(message,"KP:%f KI:%f T:%f\r\n%E,%E,%E,%E,%7.2f,%E",TRACE_KP,TRACE_KI,TRACE_T,
                    				TRACE.CurrentU,TRACE.PrevU,TRACE.AvgTimeOffset,TRACE.PrevAvgTimeOffset,PrevdDAC,KGainF);
					break;

        case    CONTROL_TGAIN_P:

                    if(sscanf(Parameter,"%f %f %f",&f1,&f2,&f3) == 3){
                        TRACE_KP = f1;  TRACE_KI = f2;   TRACE_T = f3;
	                    sprintf(message,"KP:%f KI:%f T:%f",TRACE_KP,TRACE_KI,TRACE_T);
					}
					break;

		case	MONITORCMD_P:
					if(!strcmp(Parameter,"ON") ){
						MonitorCmd = true;
					}
					else if(!strcmp(Parameter,"OFF") ){
						MonitorCmd = false;
					}
					break;

		case    CONTROL_RDAC_Q:
					sprintf(message,"RefADC: %d",ReferenceADC);
					break;

		case    CONTROL_RDAC_P:
                    if(sscanf(Parameter,"%d",&t1) == 1){
						if( t1 > 0  && t1 < 255 ){
							ReferenceADC = t1;
							sprintf(message,"RefADC: %d",ReferenceADC);
						}else{
							sprintf(message,"out of range");
						}
					}
					break;

		case	CONTROL_SYNC_:
					sprintf(message,"SYNC");
					CONTROL.SYNC = ON;
					break;

		case	CONTROL_SYNC_P:
/*
                    if( !strcmp(Parameter,"FORW") ){
						COUNTER.ShiftTxOperation = PLUS_SHIFT;
						COUNTER.ShiftTxCounter = 0; // �?ī��?3 ���Ǿ� �?.

						//Advance100ns();
                    }
                    else if(!strcmp(Parameter,"BACK")){
						COUNTER.ShiftTxOperation = MINUS_SHIFT;
						COUNTER.ShiftTxCounter = 0; // �?ī��?3 ���Ǿ� �?.

                    	//Delay100ns();
                    }
                    else{
                    	result = EXE_ERROR;
                    }
*/
					break;
/*
		case	611:// ?
					switch(CONFIG.HOLDOVER_CONTROL){

						case AGING_TEMP_CONTROL:
								sprintf(message,"AGING+TEMP");
								break;
						case TEMP_CONTROL:
								sprintf(message,"TEMP only");
								break;
					}
					break;

		case	612:
                    if( !strcmp(Parameter,"AT") ){
						CONFIG.HOLDOVER_CONTROL = AGING_TEMP_CONTROL;
                    }
                    else if(!strcmp(Parameter,"T")){
						CONFIG.HOLDOVER_CONTROL = TEMP_CONTROL;
                    }
                    CONFIG.WriteEvent = ON;
					break;
*/


		case	CONTROL_1PPSALM_Q:
					if(DIAG.PPS == FAIL){
						strcpy(message,"ON");
					} else{
						strcpy(message,"OFF");
					}
					break;

		case	CONTROL_1PPSALM_RANGE_Q:	// unit: us
					sprintf(message,"%.1f",CONFIG.AlarmRange/10.0);
					break;
		case	CONTROL_1PPSALM_RANGE_P:
                    if((sscanf(Parameter,"%f",&f1) == 1) &&
						( f1 > 0 && f1 <= 1000) &&
						( CONFIG.AlarmRange != (unsigned short)(f1*10)) ){

						CONFIG.AlarmRange = (unsigned short)(f1*10);
						CONFIG.WriteEvent = ON;

						sprintf(message,"%.1f",CONFIG.AlarmRange/10.0);
					}
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	CONTROL_HOLDOVER_DURATION_Q:
					sprintf(message,"%d min.",CONFIG.HoldoverDuration);
					break;

		case	CONTROL_HOLDOVER_DURATION_P:
                    if((sscanf(Parameter,"%d",&t1) == 1) &&
						( t1 > 0 && t1 <= 600) &&
						( CONFIG.HoldoverDuration != (unsigned short)t1) ){

						CONFIG.HoldoverDuration = (unsigned short)t1;
						CONFIG.WriteEvent = ON;

						sprintf(message,"%d min.",CONFIG.HoldoverDuration);
					}
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	CONTROL_OCXOID_Q:
					if(OCXO_ID.Valid == YES) {

						if(OCXO_ID.mode == ID_MODE_DAC ){
							sprintf(message,"Valid (Temperature)");
						}
						else if(OCXO_ID.mode == ID_MODE_CURRENT ){
							sprintf(message,"Valid (Current)");
						}
					}
					else {
						sprintf(message,"Invalid");
					}
					break;
		case	CONTROL_WARM_RANGE_Q:
					sprintf(message,"%d",CONFIG.WarmStartLimit);
					break;
		case	CONTROL_WARM_RANGE_P:
                    if((sscanf(Parameter,"%d",&t1) == 1) &&
						( t1 > 0 && t1 <= 1000) &&
						( CONFIG.WarmStartLimit != t1)){

						CONFIG.WarmStartLimit = t1;
						CONFIG.WriteEvent = ON;
					}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	CONTROL_TRACE_Q:
/*			
					if( LOCKEDtoMASTER_SPEC_IN == OK ){
						strcpy(message,"ON");
					}
					else {
						strcpy(message,"OFF");
					}
*/					
					break;

		case	CONTROL_TRACE_P:
/*			
					LOCKEDtoMASTER_SPEC_IN = OK;
					strcpy(message,"Force ON");
*/					
					break;

					
					
		case	TEST_OCXOID_Q:

					break;


		case	TEST_EB_Q:
   					break;

		case 	TEST_VERSION_Q:
				 	break;

		case	PRINT_EB_P:
                    if( !strcmp(Parameter,"ON") ){
						monitor.eb = ON;
						strcpy(message,"ON");
                    }

                    else if(!strcmp(Parameter,"OFF")){
						monitor.eb = OFF;
						strcpy(message,"OFF");
                    }
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	PRINT_DUAL_P:
			
                    if( !strcmp(Parameter,"ON") ){
						monitor.dual = ON;
						strcpy(message,"ON");
                    }
					else if(!strcmp(Parameter,"OFF")){
						monitor.dual = OFF;
						strcpy(message,"OFF");
                    }
                    else if(!strcmp(Parameter,"9927")){
                    	if(dual_print_9927){dual_print_9927 = 0;strcpy(message,"OFF");}
                    	else {dual_print_9927 = 1;strcpy(message,"ON");}
                    }
                    else{
                    	result = EXE_ERROR;
                    }
                    
					break;

		case	PRINT_HOLDOVER_P:
                    if( !strcmp(Parameter,"ON") ){
						monitor.holdover = ON;
						strcpy(message,"ON");
                    }

                    else if(!strcmp(Parameter,"OFF")){
						monitor.holdover = OFF;
						strcpy(message,"OFF");
                    }
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	TEST_OFFSET_P:

                 	if(sscanf(Parameter,"%f",&f1) == 1){
                 		TestOffset = (double)f1;
                 	}
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	RECOVERY_OFFSET_Q:
               		sprintf(message,"%.9le",Recovery_Offset);
					break;



		case	PRINT_ATABLE_:

			       	sprintf((char*)MESG,"phase:%d #%d #%d #%d\r\n=================\r\n",
        	   	        HoldoverCONTROL.Phase,
           		        HoldoverCONTROL.NUMofAgingTable,
           	    	    HoldoverCONTROL.AgingTableIndex,
           	        	HoldoverCONTROL.AgingTableSize);

       	            TransmitMessage((BYTE*)MESG,PORT_CONTROL);

					for( i = 0; i < HoldoverCONTROL.AgingTableSize ; i++ ){

				       	sprintf((char*)MESG,"<%d> %.2f %.2f %d\r\n",
		                    i,
		                    HoldoverCONTROL.AgingTable[i].DAC,
	    	                HoldoverCONTROL.AgingTable[i].TEMPER,
	        	            HoldoverCONTROL.AgingTable[i].STEP);
        	            TransmitMessage((BYTE*)MESG,PORT_CONTROL);
        	        }
               		strcpy(message,"==================");

					break;
		case	PRINT_ASOLUTION_:

		    				sprintf((char*)MESG,"%.2f %.2f %.2f : %.2f %.2f %.2f : %.2f %.2f %.2f : %.2f %.2f %.2f\r\n",
									AgingMatrix[0][0],AgingMatrix[0][1],AgingMatrix[0][2],
					     			AgingMatrix[1][0],AgingMatrix[1][1],AgingMatrix[1][2],
					     			AgingMatrix[2][0],AgingMatrix[2][1],AgingMatrix[2][2],
					     			AgingMatrix[0][3],AgingMatrix[1][3],AgingMatrix[2][3]);
				        	TransmitMessage((BYTE*)MESG,DEBUG_PORT);

    	    				sprintf((char*)MESG,"%.4f %.4f : %.4f %.4f : %.4f %.4f\r\n",
									HoldoverCalc.normal_x,HoldoverCalc.normal_y,
									HoldoverCalc.plus_x,HoldoverCalc.plus_y,
									HoldoverCalc.minus_x,HoldoverCalc.minus_y);
				        	TransmitMessage((BYTE*)MESG,DEBUG_PORT);


					sprintf(message,"SOLUTION: %.2f %.2f %.2f",Aging_sol[0],Aging_sol[1],Aging_sol[2]);
					break;
		case	HOLDOVER_SOLUTION_:

					if(HoldoverCONTROL.AgingTableSize < 10 ){
						sprintf(message,"aging table size : %d",HoldoverCONTROL.AgingTableSize);
					}
					else {
						for( i = 0; i < 4; i++) {
							for( j = 0; j < 4; j++){	AgingMatrix[i][j] = 0;	}
						}
						// �?�?��Flag set!!!
						AgingMatrix[0][0] =	(double)(HoldoverCONTROL.NumOfCal);

						HoldoverCalc.flag = ON;
						EVENT.CALCULATION = ON;	//
						HoldoverCalc.step = 0;

						strcpy(message,"aging table calc. start...");

					}
					break;


		case	DEBUG_FAIL_Q:

					TransmitMessage((BYTE*)"=================================\r\n",PORT_CONTROL);

                    if(manual_diag.rtc == OK ){    	TransmitMessage((BYTE*)"RTC : OK\r\n",PORT_CONTROL);	}
                    else {	                    	TransmitMessage((BYTE*)"RTC : FAIL\r\n",PORT_CONTROL);	}

                    if(manual_diag.osc == OK ){    	TransmitMessage((BYTE*)"OSC : OK\r\n",PORT_CONTROL);	}
                    else {	                    	TransmitMessage((BYTE*)"OSC : FAIL\r\n",PORT_CONTROL);	}

                    if(manual_diag.power == OK ){ 	TransmitMessage((BYTE*)"POWER : OK\r\n",PORT_CONTROL);	}
                    else {	                    	TransmitMessage((BYTE*)"POWER : FAIL\r\n",PORT_CONTROL);	}
					strcpy(message,"=================================");

					break;

		case	DEBUG_FAIL_RTC_P:
                    if( !strcmp(Parameter,"ON") ){
						manual_diag.rtc = FAIL;
						strcpy(message,"ON");
                    }
                    else if(!strcmp(Parameter,"OFF")){
						manual_diag.rtc = OK;
						strcpy(message,"OFF");
                    }
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	DEBUG_FAIL_OSC_P:
                    if( !strcmp(Parameter,"ON") ){
						manual_diag.osc = FAIL;
						strcpy(message,"ON");
                    }
                    else if(!strcmp(Parameter,"OFF")){
						manual_diag.osc = OK;
						strcpy(message,"OFF");
                    }
                    else{
                    	result = EXE_ERROR;
                    }
					break;

		case	DEBUG_FAIL_POWER_P:
                    if( !strcmp(Parameter,"ON") ){
						manual_diag.power = FAIL;
						strcpy(message,"ON");
                    }
                    else if(!strcmp(Parameter,"OFF")){
						manual_diag.power = OK;
						strcpy(message,"OFF");
                    }
                    else{
                    	result = EXE_ERROR;
                    }
					break;
					
		case	EDBG_P:
					/*
					if (!strcmp(Parameter,"ON")){
						EnableDebugPortTx();
						strcpy(message,"ON");
					}
					else if(!strcmp(Parameter,"OFF")){
						DisableDebugPortTx();
						strcpy(message,"OFF");
					}			
					else{
						strcpy(message,"Check!!! ON/OFF");
					}
					*/
					break;
		
		case	EDBG_Q:
					/*
					if(IsEnableDebugPortTx()){
						strcpy(message,"ON");
					}
					else {
						strcpy(message,"OFF");
					}
					*/
					break;
		
		case	ANTENNA_CHECK_P:
                    if(!strcmp(Parameter,"ON")){
						CONFIG.AntCheckFlag = YES;
						CONFIG.WriteEvent = ON;
						strcpy(message,"ON");
					}
                    else if(!strcmp(Parameter,"OFF")){
						CONFIG.AntCheckFlag = NO;
						CONFIG.WriteEvent = ON;
						strcpy(message,"OFF");
					}
					break;


		case	ANTENNA_CHECK_Q:
                    if(CONFIG.AntCheckFlag == ON){
						strcpy(message,"ON");
                    }
                    else if(CONFIG.AntCheckFlag == OFF){
						strcpy(message,"OFF");
                    }
                    else{
                    	result = EXE_ERROR;
                    }
					break;
		case	DBG_RB_:
					break;
		case	RFT_:
					break;
		case	RFS_:
					break;

		case	EB_PRINT_P:
					if( !strcmp(Parameter,"ON")){
						SetRcvDebug(ENABLE);
						rcv_debug_display = ON;
					}
					else if(!strcmp(Parameter,"OFF")){
						SetRcvDebug(DISABLE);
						rcv_debug_display = OFF;
					}
					else{
						strcpy(message,"Check!!! [On/Off]");
					}
					break;
		case 	EB_LOCK_P:
					if( sscanf(Parameter,"%d,%d,%d",&t1,&t2,&t3) == 3){
						if(t1>=0&&t1<=255){
							SetRcvThrDetect((unsigned char)t1,(unsigned char)t2,
								(unsigned char)t3);
						}
						else{
							strcpy(message,"Check!!! Parameter:3, Value:0-255, Ex)35,29,0");
						}
					}
					else{
						strcpy(message,"Check!!!! Parameter:3, Value:0-255, Ex)35,29,0");
					}
					break;
		case	EB_UNLOCK_P:
					if( sscanf(Parameter,"%d,%d,%d",&t1,&t2,&t3) == 3){
						if(t1>=0&&t1<=255){
							SetRcvThrUnlock((unsigned char)t1,(unsigned char)t2,
								(unsigned char)t3);
						}
						else{
							strcpy(message,"Check!!! Parameter:3, Value:0-255, Ex)34,21,0");
						}
					}
					else{
						strcpy(message,"Check!!!! Parameter:3, Value:0-255, Ex)34,21,0");
					}
					break;
		case	EB_PSDM_P:
					if( !strcmp(Parameter,"ON")){
						SetPSDM(ENABLE);
					}
					else if(!strcmp(Parameter,"OFF")){
						SetPSDM(DISABLE);
					}
					else{
						strcpy(message,"Check!!! [On/Off]");
					}
					break;
		case	EB_SS_P:
					strcpy(message,"Send Message to EB1 Silently");
					break;
		case	EB_SL_P:
					strcpy(message,"Send Message to EB1 Loudly");
					break;
		case	MM_DUMP_P:
					TransmitMessage((BYTE*)"\r\n== Memory Dump ==",PORT_CONTROL);
					//sprintf(MESG,"\r\n FLASH: 0x%08X, %X\r\n RAM:   0x%08X, %X", 
					//	ROM_BASE_ADDRESS, 0x80000,
					//	0x100000, 0x80000);
					//TransmitMessage((BYTE*)MESG,PORT_CONTROL);
					
					if(sscanf(Parameter,"%x %d",&t1,&t2) == 2){
                   		sprintf(MESG,"\r\n\r\nStart: 0x%08X, %d\r\n",t1, t2);
                   		TransmitMessage((BYTE*)MESG,PORT_CONTROL);
                   		
                   		LastRamDumpAddress = t1;
                   		
                        MemoryHexDump( (void * ) LastRamDumpAddress, t2 );   
                    }
                    else
                    {
                    	strcpy(message,"\r\n\r\nUsage: MM/DUMP [start-address] [count]\r\n");
                    }
					break;
		case	MM_CRC_P:
					
					break;
		case	MM_USAGE_P:
					if( !strcmp(Parameter,"ON")){
						disp_memory_usage = ON;
					}
					else if(!strcmp(Parameter,"OFF")){
						strcpy(message,"Stop Display Memory Usage");
						disp_memory_usage = OFF;
					}
					else{
						strcpy(message,"Check!!! [On/Off]");
					}
					break;
					
		case	REBOOT_:
					reset_sub_call();
					//system("reboot");
					break;
		
		case	REBOOT_P:
					///#define BAKMODE_FLASH_FILE "/gpsclock/BAKMODE"
					if(strstr(Parameter,"-B")){
						//fwup_make_file("/gpsclock/BAKMODE");
						usleep(1000);
						reset_sub_call();
						//system("reboot");
					}
					else if(strstr(Parameter,"-b")){
						//fwup_make_file("/gpsclock/BAKMODE");
						usleep(1000);
						reset_sub_call();
						//system("reboot");
					}
					else{
						sprintf(message,"reboot %s",Parameter);
						system(message);
					}
					break;



/*

		case	TANT_RESET_:
					strcpy(message,"Tant.: cold reset");
					tsip_reset_ant('K');	// cold reset
					break;

		case	TANT_RESET_P:
					if (!strcmp(Parameter,"DEFAULT")){
						strcpy(message,"Tant.: clear EEPROM");
						tsip_reset_ant('F');
					}
					break;
		case	TANT_VERSION_Q:

					sprintf(message,"M:%d %d - %d %d %d m:%d %d - %d %d %d",
										SYSTEM.EB.major_version_number,
										SYSTEM.EB.minor_version_number,

										SYSTEM.EB.version_month,
										SYSTEM.EB.version_day,
										SYSTEM.EB.version_year,

										SYSTEM.EB.major_revision_number,
										SYSTEM.EB.minor_revision_number,

										SYSTEM.EB.revision_month,
										SYSTEM.EB.revision_day,
										SYSTEM.EB.revision_year);
					break;

*/
	}

    if( (result == EXE_END) && (strlen(message) > 0)){
    	//if( strlen(Command->Output) + strlen(message) < 2000 ){
    	if( strlen(Command->Output) + strlen(message) < (300-3) ){
    		strcat(Command->Output,message);
    		strcat(Command->Output,"\r\n");
    	}
    }

    return result;

}



char GetBaudrate(char *string)
{
	int i;

	for(i = 0; i < 7; i++){
		if(!strcmp(string,BaudrateString[i])){
			return i;
		}
	}
/*
   	if( !strcmp(string,"2400") ){      		br = BR2400; }
    else if( !strcmp(string,"4800") ){  	br = BR4800; }
    else if( !strcmp(string,"9600") ){  	br = BR9600; }
    else if( !strcmp(string,"19200") ){		br = BR19200; }
    else if( !strcmp(string,"38400") ){		br = BR38400; }
    else if( !strcmp(string,"57600") ){		br = BR57600; }
    else if( !strcmp(string,"115200") ){	br = BR115200; }
*/
    return -1;
}


char * GetPositionString(P_	Position)
{

	int 	t1,t2,t3,l1,l2;
	float   f1,f2,f3;
	char    d1,d2;

	ret_buf[0] = 0;


    t1 = Position.LAT/3600000;
    t2 = (Position.LAT%3600000)/60000;
    f1 = (float)((Position.LAT%3600000)%60000)/1000.0;

    if( Position.LAT >= 0 ) {
		d1 = 'N';
	}
	else {
		d1 = 'S';
		t1 = -t1;	t2 = -t2;	f1 = -f1;
	}

    l1 = Position.LON/3600000;
    l2 = (Position.LON%3600000)/60000;
    f2 = (float)((Position.LON%3600000)%60000)/1000.0;

    if( Position.LON >= 0 ) {
		d2 = 'E';
	}
	else {
		d2 = 'W';
		l1 = -l1;	l2 = -l2;	f2 = -f2;
	}

    f3 = (float)(Position.HEI/100.0);

    sprintf(ret_buf,"%c,%d,%d,%6.3f,%c,%d,%d,%6.3f,%+7.2f",d1,t1,t2,f1,d2,l1,l2,f2,f3);
    //sprintf(MESG,"%c,%d,%d,%6.3f,%c,%d,%d,%6.3f,%+7.2f",d1,t1,t2,f1,d2,l1,l2,f2,f3);

	return ret_buf;
	//return MESG;

}


void quick_reset(void)
{
	DisableSccFPGATxRxInt();
	
	SetResetSignal();
	SetMeToSelfBlock();
	SystemReset();
	while(1);
}

extern void SystemResetCall();
void reset_sub_call(void)
{
	DisableSccFPGATxRxInt();
	
	SetResetSignal();

	//hmlee - need some delay here
    
	SetMeToSelfBlock();
	SystemReset();

	SystemResetCall();
}

void MemoryHexDump( void *address, int size )
{
	
    int lp1,lp2,lp3;
    unsigned char *ptrData;

    ptrData = (unsigned char*)address;

	//printf("\r\n");
	TransmitMessage((BYTE*)"\r\n",PORT_CONTROL);
    for( lp1 = 0; lp1 < size*4; lp1 += 16 )
    {
        sprintf(MESG, "%04X-%04X :",  (((unsigned int)ptrData)>>16)&0xFFFF,
                                 ((unsigned int)ptrData)     &0xFFFF  );
        TransmitMessage((BYTE*)MESG,PORT_CONTROL);      

        for( lp2 = 0; lp2 < 4; lp2++ )
        {
            for( lp3 = 0; lp3 < 4; lp3++ )
            {
                 sprintf(MESG, "%02X", ptrData[ lp2 * 4 + lp3 ] & 0xFF );
                 TransmitMessage((BYTE*)MESG,PORT_CONTROL);
            } 
            //printf( " " );
            TransmitMessage((BYTE*)" ",PORT_CONTROL);
        }
        //printf( "  " );
        TransmitMessage((BYTE*)" ",PORT_CONTROL);
        for( lp3 = 0; lp3 < 16; lp3++ )
        {
            if( ptrData[lp3] < ' ' ) {TransmitMessage((BYTE*)".",PORT_CONTROL);}
        	else if( ptrData[lp3] > '~' ) {TransmitMessage((BYTE*)".",PORT_CONTROL);}
            else{
            	sprintf(MESG, "%c", ptrData[lp3]  );
            	TransmitMessage((BYTE*)MESG,PORT_CONTROL);
            }
        } 

        //printf( "\r\n" );
        TransmitMessage((BYTE*)"\r\n",PORT_CONTROL);
        ptrData += 16;
    }

    LastRamDumpAddress = (unsigned long)ptrData;

}

void DisplayHelp(void)
{
	printf(" OPER/STATUS?              Formatted Status Screen\r\n");
	printf(" OPER/STATUS/DUAL?         Returns Dual Status\r\n");
	printf(" OPER/DUAL/ACTIVE X        Activates X [A or B].\r\n");
	//printf(" OPER/MANUAL ON            Start Manual Operation.\r\n");
	printf(" GPS/TIME?                 Returns System Time.\r\n");
	printf(" GPS/DATE?                 Returns System Date.\r\n");
	printf(" GPS/TRACK/SAT?            Returns a list of all sats. being tracked.\r\n");
	printf(" GPS/TRACK/SAT/COUNT?      Returns the # of all sats. being tracked.\r\n");
	printf(" GPS/POS/MODE?             Returns the current positioning mode.\r\n");
	printf(" GPS/POS/CURRENT?          Returns the current position.\r\n");
	printf(" GPS/POS/AVERAGE?          Returns the current position being averaged.\r\n");
	printf(" GPS/POS/LAST?             Returns the Latest position hold.\r\n");
	printf(" GPS/POS/HOLD N[S],x,x.x,E[W],x,x,x.x,+[-]x.x\r\n");
	printf("                           Specifies the position of the GPS Ant.\r\n");
	printf(" TIME/ZONE?                Returns the current TIME-ZONE.\r\n");
	printf(" TIME/ZONE x[,x]           Applies Local TIME-ZONE Offset.\r\n");
	printf(" TIME/MODE?                Returns the Current Timing mode[UTC or GPS].\r\n");
	printf(" TIME/MODE GPS[UTC]        Changes the Current Timing mode to GPS[UTC].\r\n");
	printf(" TIME/LEAPSECOND?          Returns LEAPSECONDs.\r\n");
	printf(" TIME/ADELAY?              Returns the current Ant. Delay value in nanosec.\r\n");
	printf(" TIME/ADELAY x             Sets the Ant. Deley in nanosec.(0 ~ 999999)\r\n");
	printf(" SYSTEM/VERSION?           Returns Sysem Version.\r\n");
	printf(" SYSTEM/INFO?              Returns System information.\r\n");
	printf(" REBOOT [-b]               Reboot System [to BAK mode].\r\n");
	

}

